var app = getApp();
var that;
var utils = require('js/utils.js');
Page({
  data: {
    motto: 'Hello World',
    totalPoint:'',
    totalContent:'',
    totalSubjects:'',
    firstProblem:'',
    badMark:'',
    badContent:'',
    secondProblem:'',
    secondBadMark:'',
    secondBadContent:'',
    goodProblem:'',
    goodMark:'',
    goodContent:'',
    teacherSubs:'',
    firstPersonAmount:'',
    secondPersonAmount:'',
    tutor_ability1:'',
    tutor_ability2:'',
    starDatas:{"yuxi":"6.0","tingke":"11.0","zuoye":"6.0","fuxi":"6.0","kaoshi":"9.0"},
	  starBelongs:['预习','听课','作业','复习','考试'],
    showStarDatas:''
  },
  onLoad:function(){
    that = this;
    that.setData({
      firstPersonAmount:app.reportResult.result.first,
      secondPersonAmount:app.reportResult.result.second,
      tutor_ability1:app.reportResult.result.tutor_ability1,
      tutor_ability2:app.reportResult.result.tutor_ability2
    });
    utils.loadFiveStar(app.openId,function(isOK,data){
        if(isOK){
            console.log(data.data.result);
            var d = data.data.result;
            var dd = [d.yuxi, d.tingke, d.zuoye, d.fuxi, d.kaoshi];
            var starDatas = that.calcStarData(that.data.starBelongs, dd);
            that.setData({
              showStarDatas: starDatas
            });
        }
        utils.loadReportcDatas(app.openId, function (isOK, data) {
          if (isOK) {
            that.loadReportOk(data);
          }
        });
    });
  },
  //这里用于处理题目的分数
  calcStarData:function(belongs,values){
	  var arrays = [];
	  for(var i=0;i<belongs.length;i++){
		  var obj = {};
		  obj.name = belongs[i];
		  obj.value = that.calcStarValue(values[i]);
		  arrays.push(obj);	
	  }
    console.log(arrays);
    return arrays;
  },
  /**
  *功能计算星的值
  *0表示没有星
  *1表示半颗星
  *2表示一颗星
  */
  calcStarValue:function(value){
	  var starStatus = [];
	  var markStar = parseInt(value/2);
	  var halfStar = value%2;
	  for(var i=0;i<5;i++){
		  if(i < markStar){//表示一颗星
			  starStatus.push(2);
		  }else if(i < markStar+halfStar){
        starStatus.push(1);
      }else{
        starStatus.push(0);
      }
	  }
	  return starStatus;
  },
  userClick:function(){
    that.jumpPage();
  },
  jumpPage:function(){
    wx.navigateTo({
      url: '../teacherList/teacherList',
      success: function(res){
        // success
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  },
  loadReportOk:function(data){
    console.log('加载报告数据');
    console.log(data);
    console.log('加载报告数据1');
    console.log(data.data.result);
    var dd = data.data.result;
    console.log('加载报告数据2');
    console.log(dd);
    console.log(dd.all_score);
    console.log(dd.all_content);
    that.setData({
      totalPoint:dd.all_score,
      totalContent:dd.all_content,
      firstProblem:dd.first_problem,
      badMark:dd.first_ability,
      badContent:dd.first_content,
      secondProblem:dd.second_problem,
      secondBadMark:dd.second_ability,
      secondBadContent:dd.second_content,
      goodProblem:dd.advantageName,
      goodMark:dd.advantageScore,
      goodContent:dd.advantageBiao,
      totalSubjects:dd.sub,
      teacherSubs:dd.sub,
      firstPersonAmount:dd.first,
      secondPersonAmount:dd.second,
      tutor_ability1:dd.tutor_ability1,
      tutor_ability2:dd.tutor_ability2
    });
  },
  loadReportFail:function(data){
      wx.showToast({
            title: '加载失败',
            mask:true,
            icon: 'cancel',
            duration: 2000
        });
  },
    onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '欢迎使用教带',
      path: 'pages/splash/splash',
      success: function(res) {
        // 转发成功
      },
      fail: function(res) {
        // 转发失败
      }
    }
  }
})